package com.ahuan.service.Impl;

import com.ahuan.domain.Dept;
import com.ahuan.domain.Role;
import com.ahuan.domain.User;
import com.ahuan.exception.BusinessException;
import com.ahuan.mapper.DeptMapper;
import com.ahuan.mapper.RoleMapper;
import com.ahuan.mapper.UserMapper;
import com.ahuan.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    DeptMapper deptMapper;


    @Override
    public List<User> queryByPage(Integer page, Integer limit) {
        //测试异常
//        if(true){
//            int a = 1/0;
//        }

        PageHelper.startPage(1, 10);
        List<User> users = userMapper.selectByPage();
        return users;
    }

    @Override
//    @Transactional
    public boolean addUser(User user) {
        userMapper.insertUser(user);
        if(user.getUserId() != null){
            return true;
        }
        return false;
    }

    @Override
//    @Transactional
    public boolean editUser(User user, String id) {
        System.out.println(id);
        return userMapper.updateUser(user) > 0;
    }

    @Override
    public boolean resetPwd(String userId) {
        return userMapper.resetPassword(userId) > 0;
    }

    @Override
    public boolean deleteUser(String userId) {
        return userMapper.deleteUserById(userId) > 0;
    }

    @Override
    public List<Role> queryRoleList() {
        return roleMapper.selectRoleList();
    }

    @Override
    public List<Dept> queryDeptList() {
        return deptMapper.selectDeptList();
    }


}
